package com.qianguo.shop.assistant;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.eva.androidlibrary.utils.IconfontUtil;
import com.eva.androidlibrary.widget.SimpleTagImageView;
import com.qianguo.shop.R;
import com.eva.androidlibrary.api.HttpRequestCallback;
import com.qianguo.shop.base.BaseTitleActivity;
import com.qianguo.shop.constant.ShopKeys;
import com.qianguo.shop.entity.ShopAssistant;
import com.qianguo.shop.entity.ShopAssistantListBean;
import com.eva.androidlibrary.icon.FruitIcon;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.UiThread.Propagation;

import java.util.ArrayList;
import java.util.List;

/**
 * 店员管理
 *
 * @author GUXIA318
 * @date 2015-11-06 15:00
 */
@EActivity(R.layout.activity_shop_assistant)
public class ShopAssistantManagerActivity extends BaseTitleActivity {

    @ViewById(R.id.shop_assistant_recycler_view)
    SwipeRefreshRecyclerView mSwipeRefreshRecyclerView;

    @Extra(ShopKeys.EXTRA_KEY_SHOP_ID)
    String mShopID;

    private static final int PER_PAGE = 10;
    private int mCurrentPage = 1;
    private int mTotalNum = -1;
    private ShopAssistantAdapter mAdapter;
    private List<ShopAssistant> mShopAssistantItems = new ArrayList<>();

    public static void jumpToShopAssistantManager(Context context, String shopID) {
        Intent intent = new Intent();
        intent.setClass(context, com.qianguo.shop.assistant.ShopAssistantManagerActivity_.class);
        intent.putExtra(ShopKeys.EXTRA_KEY_SHOP_ID, shopID);
        context.startActivity(intent);
    }

    @Override
    protected String getToolbarTitle() {
        return getString(R.string.shop_assistant_title);
    }

    @Override
    protected void initializeUI() {
        mAdapter = new ShopAssistantAdapter(this);
        mSwipeRefreshRecyclerView.setAdapter(mAdapter);

        mSwipeRefreshRecyclerView.setOnItemClickListener(new SwipeRefreshRecyclerView.OnRecyclerItemClickListener() {
            @Override
            public void onClick(int position) {
                AddOrEditShopAssistantActivity.jumpToEditShopAssistant(ShopAssistantManagerActivity.this, mShopID, mShopAssistantItems.get(position));
            }
        });

        mSwipeRefreshRecyclerView.setSwipeRefreshListener(new SwipeRefreshRecyclerView.SwipeRefreshListener() {
            @Override
            public void onLoadMore() {
                if (mCurrentPage * PER_PAGE < mTotalNum) {
                    loadData(mShopID, ++mCurrentPage, PER_PAGE);
                }
            }

            @Override
            public void onRefresh() {
                mCurrentPage = 1;
                mShopAssistantItems.clear();
                loadData(mShopID, mCurrentPage, PER_PAGE);
            }
        });

        loadData(mShopID, mCurrentPage, PER_PAGE);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_add) {
            AddOrEditShopAssistantActivity.jumpToEditShopAssistant(ShopAssistantManagerActivity.this, mShopID, null);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_add, menu);
        return true;
    }

    @Background
    void loadData(String id, int page, int perPage) {
        mApp.getApi().getShopAssistants(mApp.getUser().getToken(), id, page, perPage, new HttpRequestCallback<ShopAssistantListBean>() {
            @Override
            public void onSuccess(int code, String msg, ShopAssistantListBean data) {
                if (data != null) {
                    if (mTotalNum < 0) {
                        mTotalNum = data.getTotal();
                    }

                    if (data.getList() != null) {
                        mShopAssistantItems.addAll(data.getList());
                    }
                    renderUI();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(ShopAssistantManagerActivity.this, msg);
            }
        });
    }

    @UiThread(propagation = Propagation.REUSE)
    void renderUI() {
        mSwipeRefreshRecyclerView.notifyDataSetChanged();
    }

    public class ShopAssistantAdapter extends RecyclerView.Adapter<ShopAssistantViewHolder> {
        private Context mContext;
        public ShopAssistantAdapter(Context context) {
            this.mContext = context;
        }

        @Override
        public ShopAssistantViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_shop_assistant, null);
            v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new ShopAssistantViewHolder(v);
        }

        @Override
        public void onBindViewHolder(ShopAssistantViewHolder holder, int i) {
            ShopAssistant item = mShopAssistantItems.get(i);
            holder.mTvName.setText(item.getRealname());
            holder.mTvMobile.setText(item.getPhone());
            holder.mTvDealCount.setText(getString(R.string.shop_assistant_item_deal_count_template, item.getProcessOrderCount()));
            holder.mImgAvatar.setTagText(item.getPositionName());
            mImageLoader.loadImage(mContext, holder.mImgAvatar, item.getAvatar());
            if (getString(R.string.male).equals(item.getSex())) {
                IconfontUtil.setIcon(mContext, holder.mTvSex, FruitIcon.ICON_FEMALE);
            } else if (getString(R.string.female).equals(item.getSex())) {
                IconfontUtil.setIcon(mContext, holder.mTvSex, FruitIcon.ICON_FEMALE);
            }
        }

        @Override
        public int getItemCount() {
            return mShopAssistantItems != null ? mShopAssistantItems.size() : 0;
        }
    }

    public class ShopAssistantViewHolder extends RecyclerView.ViewHolder {

        TextView mTvName;
        TextView mTvMobile;
        TextView mTvDealCount;
        TextView mTvSex;
        SimpleTagImageView mImgAvatar;

        public ShopAssistantViewHolder(View itemView) {
            super(itemView);
            mTvSex = (TextView) itemView.findViewById(R.id.shop_assistant_sex_text_view);
            mTvName = (TextView) itemView.findViewById(R.id.shop_assistant_name_text_view);
            mTvMobile = (TextView) itemView.findViewById(R.id.shop_assistant_mobile_text_view);
            mTvDealCount = (TextView) itemView.findViewById(R.id.shop_assistant_deal_count_text_view);
            mImgAvatar = (SimpleTagImageView) itemView.findViewById(R.id.shop_assistant_avatar_image_view);
        }
    }
}
